package com.lai.gulimall.order.web;

import com.lai.common.constant.OrderConstant;
import com.lai.gulimall.order.service.OrderService;
import com.lai.gulimall.order.vo.OrderSettleVo;
import com.lai.gulimall.order.vo.OrderSubmitRespVo;
import com.lai.gulimall.order.vo.OrderSubmitVo;
import org.bouncycastle.math.raw.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;


    /**
     * 确认订单
     * @param model
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderSettleVo orderSettleVo = orderService.confirmOrder();
        model.addAttribute("orderSettleData",orderSettleVo);
        return "settle";
    }


    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model){
        //下单：创建订单，验令牌，验价格，锁库存
        OrderSubmitRespVo orderSubmitRespVo = orderService.submitOrder(orderSubmitVo);
        if(orderSubmitRespVo.getCode() == OrderConstant.OrderSubmitEnum.SUCCESS.getCode()){
            model.addAttribute("orderSubmitRespVo",orderSubmitRespVo);
            return "pay";
        }else{
           /* switch (orderSubmitRespVo.getCode()){
                case OrderConstant.OrderSubmitEnum.TOKEN_VERIFY_ERROR.getCode(): OrderConstant.OrderSubmitEnum.TOKEN_VERIFY_ERROR.getMsg();
            }*/
            return "redirect:http://order.gulimall.com//toTrade";
        }

    }
}
